package pl.edu.agh.student.model;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import org.hibernate.validator.Email;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotNull;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.security.management.UserEnabled;
import org.jboss.seam.annotations.security.management.UserPassword;
import org.jboss.seam.annotations.security.management.UserPrincipal;
import org.jboss.seam.annotations.security.management.UserRoles;

@Entity
@Name("user")
@Table(name = "users",
uniqueConstraints = {@UniqueConstraint(columnNames={"username"})})
public class User extends AbstractEntity {

    private String username;
    private String password;
    private String email;  
    private boolean enabled;
    private Customer person;

    public User(){
        super();
    }
        
    public User(String username, Customer person) {
        this();
        this.username = username;
        this.person = person;
    }

    @OneToOne(optional=false, mappedBy="user")
    public Customer getPerson() {
        return person;
    }

    public void setPerson(Customer person) {
        this.person = person;
    }

    @UserPassword(hash = "md5")
    @Column(name="password", nullable=true)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    @NotNull
    @Length(min=5)
    @UserPrincipal
    @Column(name="username", nullable=false)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @UserEnabled
    @Column(name="enable", nullable=true)
    public boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    
    @Email
    @Column(name="email", nullable=true)
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    private List<Role> roles = new LinkedList<Role>();
    
//    @JoinTable(name = "UserRoles",
//    joinColumns =
//    @JoinColumn(name = "Id"),
//    inverseJoinColumns =
//    @JoinColumn(name = "Id"))
    
    @UserRoles
    @ManyToMany(mappedBy="users", targetEntity=Role.class)
    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }
    
    public String rolesName(){
    	if(roles == null || roles.size() == 0)
    		return "";
    	StringBuffer sb = new StringBuffer();
    	for(Role r : roles)
    		sb.append(r.getName()+" ");
    	return sb.toString().trim();
    }
    
}
